package selevert;
import com.alibaba.fastjson.JSONObject;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.util.ArrayList;
public class All {
    private static final String user = "root";
    private static final String pwd = "xflovexz";
    private static final String url = "jdbc:mysql://localhost:3306/info_all?useSSL=false&useUnicode=true&characterEncoding=utf8";

    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            System.out.println("注册失败");
            e.printStackTrace();
        }
    }


    public static Connection getConn() {
        Connection conn = null;// alt+回车
        try {
            conn = DriverManager.getConnection(url, user, pwd);
        } catch (SQLException e) {
            System.out.println("获取连接对象失败");
            e.printStackTrace();
        }
        return conn;
    }
    public static ResultSet select(String sql, String... keys) {
        ResultSet res = null;
        try {
            Connection coon = getConn();
            PreparedStatement pre = coon.prepareStatement(sql);
            for (int i = 0; i < keys.length; i++) {
                pre.setString((i + 1), keys[i]);
            }
            res = pre.executeQuery();
        } catch (SQLException e) {
            System.out.println("查询失败");
            e.printStackTrace();
        }
        return res;
    }

    public static int updata(String sql, String... keys) {
        int num = 0;
        try {
            Connection conn = getConn();
            // 预编译对象
            PreparedStatement pre = conn.prepareStatement(sql);
            for (int i = 0; i < keys.length; i++) {
                pre.setString((i + 1), keys[i]);
            }
            num = pre.executeUpdate();
        } catch (SQLException e) {
            System.out.println("更新失败");
            e.printStackTrace();
        }
        return num;
    }

    public static void delete(String sql, String... keys) {
        int num = 0;
        try {
            Connection conn = getConn();
            PreparedStatement pre = conn.prepareStatement(sql);
            for (int i = 0; i < keys.length; i++) {
                pre.setString((i + 1), keys[i]);
            }
            num = pre.executeUpdate();
            if (num > 0) {
                System.out.println("删除成功");
            } else {
                System.out.println("删除失败");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static String[] add(String sql, String... keys) {
        try {
            Connection conn = getConn();
            PreparedStatement pre = conn.prepareStatement(sql);
            for (int i = 0; i < keys.length; i++) {
                pre.setString((i + 1), keys[i]);
            }

            int num = pre.executeUpdate();
            System.out.println("=========");
            System.out.println(num);
            if (num>0){
                System.out.println("添加成功");
            }
        } catch (SQLException e) {
            e.printStackTrace();
            System.out.println("禁止输入相同id");

        }
        return keys;
    }
    @WebServlet("/select")
    public static class K extends HttpServlet {
        All_info allInfo1 = null;
        ArrayList<All_info> arrayList = null;
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.setContentType("text/html;charset=utf-8");
            PrintWriter out = resp.getWriter();
            String sql = "select *from info i  INNER JOIN two t  ON i.id  = t.id";
            Connection connection = All.getConn();
            ResultSet res = All.select(sql);
            arrayList = new ArrayList<>();
            try {
                while (res.next()) {
                    int id = res.getInt("id");
                    String name = res.getString("name");
                    String modal = res.getString("modal");
                    String color = res.getString("color");
                    allInfo1 = new All_info(id, name, modal,color);
                    arrayList.add(allInfo1);
                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            req.setAttribute("all",arrayList);
            req.getRequestDispatcher("/index.jsp").forward(req,resp);
        }
        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            req.setCharacterEncoding("utf-8");
            int id = Integer.parseInt(req.getParameter("new_id"));
            String pinpai = req.getParameter("pinpai");
            String modal = req.getParameter("modal");
            String color = req.getParameter("color");
            JSONObject json = new JSONObject();
            AllFeatures allFeatures = new AllFeatures();
            allFeatures.add(id,modal,color);
            allFeatures.new_add(id,pinpai);
            json.put("status","success");
            resp.setContentType("application/json;charset=UTF-8");
            resp.getWriter().write(json.toString());
        }
    }
    @WebServlet("/edit")
    public static class L extends HttpServlet{
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.setContentType("text/html;charset=utf-8");
            int id = Integer.parseInt(req.getParameter("uid"));
            AllFeatures allFeatures = new AllFeatures();
            OneInfo oneInfo = allFeatures.select2(id);
            JSONObject json = new JSONObject();
            json.put("status","success");
            json.put("info",oneInfo);
            resp.setContentType("application/json;charset=UTF-8");
            resp.getWriter().write(json.toString());
        }

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        }
    }
    @WebServlet("/edit/save")
    public static class H extends HttpServlet{
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        }

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            resp.setContentType("text/html;charset=utf-8");
            int id = Integer.parseInt(req.getParameter("id1"));
            String name = req.getParameter("name1");
            String gender = req.getParameter("gender1"); // 型号modal
            JSONObject json = new JSONObject();
            json.put("status","success");
            AllFeatures allFeatures =new AllFeatures();
            allFeatures.updata2(gender,name, String.valueOf(id));
            resp.setContentType("application/json;charset=UTF-8");
            resp.getWriter().write(json.toString());
        }
    }
    @WebServlet("/delete")
    public static class G extends HttpServlet{
        @Override
        protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            AllFeatures allFeatures = new AllFeatures();
            int uid = Integer.parseInt(req.getParameter("uid"));
            System.out.println(uid);
            allFeatures.delete1(uid);
            allFeatures.delete(uid);
            resp.setContentType("text/html;charset=utf-8");
            JSONObject json = new JSONObject();
            json.put("status","success");
            resp.setContentType("application/json;charset=UTF-8");
            resp.getWriter().write(json.toString());
        }

        @Override
        protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        }
    }
}
